<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	set_html('title','在线支付');
	$return['merchid']=$merchid=$_RQ['merchid'];
	if(!$merchid){
		exi('参数错误','error');
	}
	$return['getmerchopenid']=$this->model('member')->apiget_openid($merchid,$_AR['access_json']);
	$merch=pdo_get('jy_merch_user',array('id'=>$merchid));
	$return['merch']=$merch;
	exi($return);
}
if($_RQ['op']=='post'){
		$postdata=get_postdata();
		$merchid=$postdata['merchid'];
		if($postdata['money']<=0){
			exi('请输入支付金额！','error');
		}
		$data=array(
			'ordersn'=>'SN'.get_ordersn('jy_merch_paylog','ordersn'),
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'merchid'=>$merchid,
			'money'=>$postdata['money'],
			'createtime'=>time(),
			'status'=>0,
		);
		if($this->setting['isone_credit']){
			$data['clearstatus']=5;
		}
		pdo_insert('jy_merch_paylog',$data); 
		$oid=pdo_insertid();	
		$params=array('title'=>'在线付款','fee'=>$postdata['money'],'id'=>$oid,'plugin'=>'jy_merch');
		if($this->setting['isone_credit']){
			$params['merchid']=$merchid;
		}
		$params['back_url']='jy_merch/inpay/payresult/';
		$return['pay_id']=cfc('pay')->create_pay_order($params);
		pdo_update('jy_merch_paylog',array('pay_id'=>$return['pay_id']),array('id'=>$oid));
		exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('收款失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('收款失败！','error');
	}
	$order=pdo_get('jy_merch_paylog',array('id'=>$payresult['id']));
	if(!$order){
		exi('收款失败！订单不存在！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('收款失败！订单金额不匹配！','error');
	}
	$update=array('status'=>1,'paytime'=>time(),'pay_type'=>$payresult['pay_type'],'paysn'=>$payresult['transaction_id']);
	if(($update['pay_type']=='wechat' || $update['pay_type']=='credit') && $payresult['onemerchid']){
		$update['clearstatus']=5;
	}
	pdo_update('jy_merch_paylog',$update,array('id'=>$order['id']));
	$order['paytype']=$payresult['pay_type'];
	$order['price']=$order['money'];
	$this->model('order')->buy_credit3($order,true);
	if($this->zhaoshang['is_downfee']){
		$order['codepay']=1;
		$this->model('commission')->get_zhaoshangorder($order);
		$this->model('commission')->order_complete($order);
	}
	if($this->plugin_exists('jy_paimerch')){
		$jy_paimerch=plugin('jy_paimerch')->plugin_setting('basic');
		if($jy_paimerch['isopen']){
			$order['tablename']='jy_merch_paylog';
			model('jy_paimerch/pai')->create($order);
		}
	}
	if($this->plugin_exists('jy_newpaimerch')){
		$jy_newpaimerch=plugin('jy_newpaimerch')->plugin_setting('basic');
		if($jy_newpaimerch['isopen']){
			$order['tablename']='jy_merch_paylog';
			model('jy_newpaimerch/pai')->create($order,1);
		}
	}
	exi('收款成功','success');
}
